package com.icss.OA.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.icss.OA.common.ServiceException;
import com.icss.OA.pojo.Emp;
import com.icss.OA.common.R;
import com.icss.OA.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * CURD:员工
 */
@RestController
public class EmpController {


    @Autowired
    EmpService empService;
    /**
     * 查询功能
     * @param condition 条件
     * @return
     */
    @GetMapping("emp.action")
    public R<Emp> search(Emp condition) {

        PageHelper.startPage(condition.getPageNum(), condition.getPageSize());

        // 调用service的查询方法
        List<Emp> elist = empService.findByCondition(condition);

        PageInfo<Emp> page = new PageInfo<>(elist);
        return R.success(page);
    }

    @DeleteMapping("{id}/emp.action")
    public R delete(@PathVariable("id") Integer empNo) {

        try {
            empService.delete(empNo);
        } catch (ServiceException e) {
            e.printStackTrace();
            return R.fail(e);
        }

        return R.success("删除成功");
    }

    @PostMapping("emp.action")
    public R addEmp(@RequestBody Emp emp) {

        try {
            empService.inertEmp(emp);
        } catch (ServiceException e) {
            e.printStackTrace();
            return R.fail(e);
        }

        return R.success("插入成功！");
    }

    @PutMapping("emp.action")
    public  R modify(@RequestBody Emp emp) {

        empService.modifyEmp(emp);
        return  R.success("更新成功！");
    }
}
